package es.ats.codington.web.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.services.EventSignUpService;

/**
 * Servlet implementation class ViewEventsofUserServlet
 */
@WebServlet("/ViewEventsofUserServlet")
public class ViewEventsofUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewEventsofUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");  
		
		EventSignUpService es=new EventSignUpService();
		HttpSession session=request.getSession(true);
		List<Event> eventList = null;
		
		try {
			
			EventSignUp e=new EventSignUp();
			e.setIdVisitor((Integer)session.getAttribute("idVisitor"));
			
			eventList = es.viewEventsOfUser(e);
			
			session.setAttribute("eventSignUpList",eventList);
			if ((Integer)session.getAttribute("isAdmin")==1){
				RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    		rd.forward(request,response);
			}
			else{
				RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    		rd.forward(request,response);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");  
		
		EventSignUpService es=new EventSignUpService();
		HttpSession session=request.getSession(true);
		List<Event> eventList = null;
		
		try {
			
			EventSignUp e=new EventSignUp();
			e.setIdVisitor((Integer)session.getAttribute("idVisitor"));
			
			eventList = es.viewEventsOfUser(e);
			
			session.setAttribute("eventSignUpList",eventList);
			if ((Integer)session.getAttribute("isAdmin")==1){
				RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    		rd.forward(request,response);
			}
			else{
				RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    		rd.forward(request,response);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
		
	}
}

